<?php

/**
 * IndexController - The default controller class
 *
 * @author
 * @version
 */

require_once 'Zend/Controller/Action.php';

class Administrator_BrandlogoController extends Zend_Controller_Action
{



    public function init()
    {

        $member=new CMember();

        if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName()))
        {

            $this->_redirect(MY_URL.'/administrator-auth-login');

        }
        
    	$memberArray = $member->getUserDetails(array('uid' => $_COOKIE[COOKIE_PRE.'uid']));
    	if($_COOKIE[COOKIE_PRE.'uid']=='1'){
				
		} else {
        	$strarray = explode(",", $memberArray[0]['menuflag']);
        	
        	if (!in_array('2', $strarray)) {
        		exit('no priviage');
        	}
		}

    }

    public function preDispatch()
    {

        $this->getHelper ('layout')->setLayout('framelayout');

        $this->view->addHelperPath(MY_ROOT.'/vhelpers');

        Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');

    //$this->getHelper('User')->getinfo($this->view,$this->_request->getModuleName(),$this->_request->getControllerName());

    }

    public function confirmcodeAction()
    {

        $this->getHelper ('layout')->disableLayout ();

        $this->_helper->viewRenderer->setNoRender();

        $this->view->confirmCode('administratorConfirmCode');

    }


    public function indexAction()
    {

        $Cbrandlogo=new Cbrandlogo();

        $brandlogoArray=$Cbrandlogo->getAllbrandlogo();

        $this->view->assign('brandlogoArray',$brandlogoArray);
    }

    public function getitemajaxAction()
    {

        $this->getHelper ('layout')->disableLayout ();

        $flid=$this->_request->getParam('flid');

        $searchQuery=$this->_request->getParam('searchQuery','');

        $page=$this->_request->getParam('page',1);

        Zend_Session::start();

        $sessionPage=new Zend_Session_Namespace('productPage');

        $sessionPage->page=$page;


        $searchArray=json_decode(stripslashes($searchQuery),true);

        $where=array();

        if($searchArray['time_from']!='')
        {

            $searchArray['time_from']=strtotime($searchArray['time_from']);

            $where[]="add_time>'".$searchArray['time_from']."'";

        }

        if($searchArray['time_to']!='')
        {

            $searchArray['time_to']=strtotime($searchArray['time_to']);

            $where[]="add_time<'".$searchArray['time_to']."'";

        }


        if($searchArray['seller']!='')
        {

            $member=new CMember();

            $userArray=$member->getUserDetails(array('username'=>$searchArray['seller']));

            $where[]=R_PRE."items.uid='".$userArray[0]['uid']."'";

        }

        if($searchArray['min_price']!='')
        {

            $where[]="min_price>='".$searchArray['min_price']."'";

        }

        if($searchArray['max_price']!='')
        {

            $where[]="max_price<='".$searchArray['max_price']."'";

        }

        if($searchArray['type']!='')
        {

            $where[]="type='".$searchArray['type']."'";

        }

        if($searchArray['keyword']!='')
        {

            $where[]="subject LIKE '%".$searchArray['keyword']."%'";

        }

        $isshow=$this->_request->getParam('isshow');

        //echo $where;

        $item=new CItem();


        if($isshow=='')
        {

            $where=join(' AND ',$where);

            $itemCounts=$item->getAllItemCounts('product',$flid,0,'has_banned',$where);

        }else
        {

            $where[]=R_PRE."products.is_showed='$isshow'";

            $where=join(' AND ',$where);

            $itemCounts=$item->getAllItemCounts('product',$flid,0,'has_banned',$where);

        }

        Zend_Loader::loadClass("CFilter");

        $pageArray = CFilter::filterPage ( $page, $itemCounts, 10 );

        $itemArray=$item->getAllItems($pageArray['from'],10,'product',$flid,0,'has_banned',$where);

        Zend_Loader::loadClass("CPage");

        $pageString = CPage::commonDivAjax($pageArray ['pageNumber'], $pageArray ['page'], 'productEffection.showAll',$flid); //生成分页字符串

        $this->view->assign ( "pageString", $pageString );

        $this->view->assign ( "pageArray", $pageArray );

        $this->view->assign('itemArray',$itemArray);
		/*
		$feature=new CFeature();
		
		$featureArray=$feature->getAllFeatures(0,0);
		
		$this->view->assign('featureArray',$featureArray);
    	*/
    }

    public function dofeatureAction()
    {

        $this->_helper->viewRenderer->setNoRender();

        $itemidArray=$this->_request->getParam('itemArray');

        $data=array(
            'featureid'=>$this->_request->getParam('featureid')
        );

        $product=new CProduct();

        foreach($itemidArray as $itemid)
        {

            $product->editProduct($itemid,$data);

        }

        $this->view->showMsg("设置成功!",'administrator'.CHOSING_DE.'product'.CHOSING_DE.'index');

    }

    public function addAction()
    {
        $itemid=$this->_request->getParam('itemid');

        $this->view->assign('itemid',$itemid);
    }

    public function doaddAction()
    {

        $this->_helper->viewRenderer->setNoRender();

        if(!$this->getRequest()->isPost())
        {

            $this->view->showMsg("发生未知错误!");

            return false;

        }

        $thisArray=$this->_request->getParams();
        
        $filters = array(
            '*'   => 'StringTrim'
        );

        $validators = array(
        );

        $input = new Zend_Filter_Input($filters, $validators, $thisArray);

	/*图片附件 start*/
        $cut_url = $_POST[imgUrl];
	/*图片附件 end*/
        
        $content=htmlspecialchars_decode($_POST[yinfo]);

        $content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);

    	/*插入到公告表 start*/
        $Cbrandlogo=new Cbrandlogo();
        $data=array(
            'carrier'=>$input->carrier,
            'carrier_cn'=>$input->carrier_cn,
            'sinfo'=>trim($content),
            'cut_url'=>trim($cut_url[count($cut_url)-1]),
            'openflag'=>trim($_POST[openflag])
        );
        $Cbrandlogo->createbrandlogo($data);
		/*插入到公告表 end*/

        $this->view->showMsg("添加成功!",'administrator-brandlogo-index');

        return true;

    }


    public function addAttachment($input,$itemid)
    {

        $attachmentData=array();

        $path=new CPath();

        //$image=new CImage();

        $img=new CClsImage();

        Zend_Loader::loadClass('CFile');

        $cut_url='';

        for($i=0;$i<count($input->imgUrl);$i++)
        {

            $newPath=$path->toDbPath('item',$input->imgUrl[$i]);//生成新的item图片路径

            CFile::copyAndUnlink($path->toAbsoluteRootPath($input->imgUrl[$i]),$path->toAbsoluteRootPath($newPath));//从临时文件夹转到item图片文件夹

            //$cutPath=$image->cutPhoto($newPath, '250', $path->getPathFromType('cut_item'));//截图

            $paramArray=$this->_request->getParams();

            $paramArray=$this->_request->getParams();
            if($paramArray["thumbcaek"])
            {
                $cutPath=$img->make_thumb($newPath,200,200,$path->getPathFromType('cut_item'));
            }else
            {
                $cutPath= str_replace("/item_img/","/cut_item_img/",$newPath)  ;
                copy(realpath( './' )."/".$newPath,realpath( './' )."/".$cutPath);
            }

            $attachmentData[$i]['itemid']=$itemid;

            $attachmentData[$i]['attachment_url']=$newPath;

            $attachmentData[$i]['cut_url']=$path->toDbPath('cut_item',$cutPath);

            $attachmentData[$i]['intro']=$input->imgIntro[$i];

            if($input->imgUrl[$i]==$input->cut_url)
            {

                $cut_url=$attachmentData[$i]['cut_url'];

            }

        }

        return array($attachmentData,$cut_url);

    }


    public function addPrice($input,$itemid)
    {

        $productPrice=new CProductPrice();

        $patternArray=$input->pattern_name;

        $priceArray=array();

        for($i=0;$i<count($patternArray);$i++)
        {

            $data=array(
                'itemid'=>$itemid,
                'min_counts'=>$input->min_counts[$i],
                'max_counts'=>$input->max_counts[$i],
                'supply_price'=>$input->supply_price[$i],
                'net_price'=>round($input->supply_price[$i]*SELL_RATE,2),
                'delivery_days'=>$input->delivery_days[$i],
                'pattern_name'=>$input->pattern_name[$i],
            );

            $priceArray[]=round($input->supply_price[$i]*SELL_RATE,2);

            $productPrice->createPrice($data);
        }

        return $priceArray;

    }

    public function editAction()
    {

        $pid=$this->_request->getParam('pid');

        $Cbrandlogo=new Cbrandlogo();

        $onebrandlogoArray=$Cbrandlogo->getDetail($pid);

        $this->view->assign('onebrandlogoArray',$onebrandlogoArray);

    }

    public function doeditAction()
    {

        $this->_helper->viewRenderer->setNoRender();

        if(!$this->getRequest()->isPost())
        {

            $this->view->showMsg("发生未知错误!");

            return false;

        }

        $paramArray=$this->_request->getParams();

        $itemid=$paramArray['itemid'];

        $pid=$paramArray['pid'];

        $filters = array(
            '*'   => 'StringTrim'
        );

        $validators = array(
        );

        $input = new Zend_Filter_Input($filters, $validators, $paramArray);
        

		/*图片附件 start*/
        $cut_url = $_POST[imgUrl];
		/*图片附件 end*/

        $content=htmlspecialchars_decode($_POST[yinfo]);

        $content=str_replace('html/upload/temp_img/','html/upload/item_img/',$content);
       
         	/*修改公告表 start*/

        $Cbrandlogo=new Cbrandlogo();
        $data=array(
            'carrier'=>$input->carrier,
            'carrier_cn'=>$input->carrier_cn,
            'sinfo'=>trim($content),
            'cut_url'=>trim($cut_url[count($cut_url)-1]),
            'openflag'=>trim($_POST[openflag])
        );

        ///die($_POST[flname]);
        $Cbrandlogo->editbrandlogo($pid,$data);

		/*修改公告表 end*/

        $this->view->showMsg("修改成功!",'administrator-brandlogo-index');

        return true;

    }

    public function editAttachment($input,$itemid)
    {

        $attachmentData=array();

        $path=new CPath();

        //$image=new CImage();

        $img=new CClsImage();

        Zend_Loader::loadClass('CFile');

        $cut_url='';

        $my_cut_url='';

        for($i=0;$i<count($input->imgUrl);$i++)
        {

            $attachmentData[$i]['attachmentid']=$input->attachmentid[$i];

            if($attachmentData[$i]['attachmentid']=='')
            {

                $newPath=$path->toDbPath('item',$input->imgUrl[$i]);//生成新的item图片路径

                CFile::copyAndUnlink($path->toAbsoluteRootPath($input->imgUrl[$i]),$path->toAbsoluteRootPath($newPath));//从临时文件夹转到item图片文件夹

                //$cutPath=$image->cutPhoto($newPath, '250', $path->getPathFromType('cut_item'));//截图
                $paramArray=$this->_request->getParams();
                if($paramArray["thumbcaek"])
                {
                    $cutPath=$img->make_thumb($newPath,200,200,$path->getPathFromType('cut_item'));
                }else
                {
                    $cutPath= str_replace("/item_img/","/cut_item_img/",$newPath)  ;
                    copy(realpath( './' )."/".$newPath,realpath( './' )."/".$cutPath);
                }

                $cut_url=$path->toDbPath('cut_item',$cutPath);

                $attachmentData[$i]['attachment_url']=$newPath;

                $attachmentData[$i]['cut_url']=$cut_url;

                $attachmentData[$i]['itemid']=$itemid;

                if($input->imgUrl[$i]==$input->cut_url)
                {

                    $my_cut_url=$attachmentData[$i]['cut_url'];

                }

            }else
            {

                $my_cut_url=$input->cut_url;

                $my_cut_url=$my_cut_url==null?'':$my_cut_url;

            }

            $attachmentData[$i]['intro']=$input->imgIntro[$i];

        }
        return array($attachmentData,$my_cut_url);

    }
    
    public function uploadimgAction()
    {

        header("Content-type: text/html; charset=utf-8");

        $this->getHelper ('layout')->disableLayout ();

        $upload=new CUpload();

        $path=new CPath();

        $thisPath=$path->getPathFromType('temp');

        $imageArray=$upload->uploadFile($thisPath,null,null,'500000');

        $isSuccess=false;

        if(is_array($imageArray))
        {

            $isSuccess=true;

            $imgUrl=$path->toDbPath('temp',$imageArray[0]);

            $this->view->assign('imgUrl',$imgUrl);

        }else
        {

            $error=$imageArray;

            $this->view->assign('error',$error);

        }

        $this->view->assign('isSuccess',$isSuccess);

    }

public function dodelAction(){

    	$this->_helper->viewRenderer->setNoRender();

    	$pid=$this->_request->getParam('pid');

    	if($pid==''){
    		$itemidArray=$this->_request->getParam('itemArray');
    	}else{
    		$itemidArray=array($pid);
    	}

        $Cbrandlogo=new Cbrandlogo();

    	$Cbrandlogo->delbrandlogo($itemidArray);

    	$this->view->showMsg('删除成功!');

    }

    public function setshowAction()
    {

        $this->_helper->viewRenderer->setNoRender();

        $isshow=$this->_request->getParam('isshow');

        $itemidArray=$this->_request->getParam('itemArray');

        $product=new CProduct();

        if($isshow==1||$isshow==0)
        {

            for($i=0;$i<count($itemidArray);$i++)
            {

                $product->editProduct($itemidArray[$i],array('is_showed'=>$isshow));

            }

            $this->view->showMsg('设置成功!');

        }else
        {

            $this->view->showMsg('设置有误!');

        }


    }

    public function postAction()
    {

    //$this->_helper->viewRenderer->setNoRender();

    //$this->getHelper ('layout')->disableLayout ();

    }



}
